import mongoose from "mongoose";

// mongo 连接
const connect = async () => {
    try {
        // 需要创建.env文件吗？
        // 不需要，next.config.mjs中配置即可?
        // todo: write as .env file
        if (process.env.NODE_ENV === 'production') {
            await mongoose.connect(process.env.MONGODB_URL);
        } else {
            await mongoose.connect('mongodb://localhost:27017/nextjs-learn');
        }
        console.log('mongodb连接成功');
    } catch (error) {
        console.log('mongodb连接失败', error);
    }
}

export default connect;